package org.techmore.activity;

import org.techmore.R;
import org.techmore.biz.MyCouponDetailsActivityDataHandler;
import org.techmore.vo.CouponDetailsVO;
import org.techmore.zxing.BarcodeFormat;
import org.techmore.zxing.WriterException;
import org.techmore.zxing.client.android.CaptureActivity;
import org.techmore.zxing.client.android.encode.QRCodeEncoder;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MyCouponDetailsActivity extends Activity {
	
	private String couponId = null;
	private String couponNo = null;
	
	private Animation animation = null;
	
	private LinearLayout linearLayout = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		animation = initAnimation();
		setScreenOrientation();
		linearLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.techmore_coupondetails, null);
		adapterContentView(linearLayout);
		adapterFootButton(linearLayout);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		couponId = getIntent().getStringExtra("couponId");
		couponNo = getIntent().getStringExtra("couponNo");
		fillData(linearLayout);
		setBackButton(linearLayout);
		setDeleteButton(linearLayout);
	}
	
	//deleteCouponButton  coupon_back_button
	
	private void setBackButton(LinearLayout linearLayout){
		Button coupon_back_button = (Button) linearLayout.findViewById(R.id.coupon_back_button);
		coupon_back_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				postStartActivity(MyCouponDetailsActivity.this,MyCouponListActivity.class);
			}
		});
	}
	
	private AlertDialog alertDialog = null;
	
	private void initDialog(){
		AlertDialog.Builder ab = new AlertDialog.Builder(this);
		ab.setTitle("警告");
		ab.setMessage("是否確定刪除？");
		ab.setNegativeButton(" 否 ", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		}).setPositiveButton(" 是 ", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				boolean delete = MyCouponDetailsActivityDataHandler.getInstance().deleteCoupon(MyCouponDetailsActivity.this, couponId, couponNo);
				if(delete){
					Toast.makeText(MyCouponDetailsActivity.this, "刪除成功", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(MyCouponDetailsActivity.this, "刪除失敗", Toast.LENGTH_SHORT).show();
				}
				alertDialog.dismiss();
				postStartActivity(MyCouponDetailsActivity.this,MyCouponListActivity.class);
			}
		});
		alertDialog = ab.create();
	}
	
	
	private void setDeleteButton(LinearLayout linearLayout){
		Button deleteCouponButton = (Button) linearLayout.findViewById(R.id.deleteCouponButton);
		initDialog();
		deleteCouponButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				alertDialog.show();
			}
		});
	}
	
	@SuppressWarnings("rawtypes")
	private void postStartActivity(Activity startActivity,Class toClazz) {
		Intent intent = new Intent(startActivity,toClazz);
		MyCouponDetailsActivity.this.startActivity(intent);
		overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		MyCouponDetailsActivity.this.finish();
	}
	
	private void adapterFootButton(LinearLayout linearLayout){
		//獲取屏幕高度
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        Integer windowWidth = displayMetrics.widthPixels;
        
    	LinearLayout couponListButton = (LinearLayout) linearLayout.findViewById(R.id.couponListButton);
        LinearLayout nearCouponListButton = (LinearLayout) linearLayout.findViewById(R.id.nearCouponListButton);
    	LinearLayout myCouponListButton = (LinearLayout) linearLayout.findViewById(R.id.myCouponListButton);
    	LinearLayout couponScanner = (LinearLayout) linearLayout.findViewById(R.id.couponScanner);
    	
    	RelativeLayout.LayoutParams layoutParams = null;
    	
    	if(240<windowWidth){
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) couponListButton.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		couponListButton.setLayoutParams(layoutParams);
    		
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) nearCouponListButton.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		nearCouponListButton.setLayoutParams(layoutParams);
    		
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) myCouponListButton.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		myCouponListButton.setLayoutParams(layoutParams);
    		
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) couponScanner.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		couponScanner.setLayoutParams(layoutParams);
    	}
			
		couponListButton.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				postStartActivity(MyCouponDetailsActivity.this, CouponListActivity.class);
				return false;
			}
		});
		
		nearCouponListButton.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(MyCouponDetailsActivity.this, CouponMapActivity.class);
				MyCouponDetailsActivity.this.startActivity(intent);
				overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
//				CouponListActivity.this.finish();
				return false;
			}
		});
		
		
		couponScanner.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MyCouponDetailsActivity.this);
				if(preferences.getBoolean("isLogin",false)){
					Intent intent = new Intent();
					intent.setClass(MyCouponDetailsActivity.this, CaptureActivity.class);
					MyCouponDetailsActivity.this.startActivity(intent);
					overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
				}else{
					MyCouponDetailsActivity.this.startActivity(new Intent(MyCouponDetailsActivity.this,CouponLoginActivity.class));
				}
				return false;
			}
		});
		
		myCouponListButton.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				postStartActivity(MyCouponDetailsActivity.this, MyCouponListActivity.class);
				return false;
			}
		});
	   }
	
	
	private void fillData(LinearLayout linearLayout){
		
		TextView coupondetailsubject = (TextView) linearLayout.findViewById(R.id.coupondetailsubject);
		TextView coupondetailstore = (TextView) linearLayout.findViewById(R.id.coupondetailstore);
		ImageView coupondetails_imageview = (ImageView) linearLayout.findViewById(R.id.coupondetails_imageview);
		
		TextView beginDateTextView = (TextView) linearLayout.findViewById(R.id.beginDateTextView);
		TextView endDateTextView = (TextView) linearLayout.findViewById(R.id.endDateTextView);
		
		TextView commentTextView = (TextView) linearLayout.findViewById(R.id.commentTextView);
		
		CouponDetailsVO couponDetailsVO = MyCouponDetailsActivityDataHandler.getInstance().getCouponDetailsVO(this, couponId, couponNo);
		
		coupondetailsubject.setText(couponDetailsVO.getSubject());
		coupondetailsubject.setAnimation(animation);
		
		coupondetailstore.setText(couponDetailsVO.getStore());
		coupondetailstore.setAnimation(animation);
		
		beginDateTextView.setText(couponDetailsVO.getStartDate());
		beginDateTextView.setAnimation(animation);
		
		
		if(null == couponDetailsVO.getEndDate() || couponDetailsVO.getEndDate().equals("")){
			endDateTextView.setText(R.string.couponValidityTime);
		}else{
			endDateTextView.setText(couponDetailsVO.getEndDate());
		}
		endDateTextView.setAnimation(animation);
		
		commentTextView.setText(couponDetailsVO.getIntroduction());
		commentTextView.setAnimation(animation);
		
		try {
			Bitmap bitmap = QRCodeEncoder.encodeAsBitmap(couponDetailsVO.getCouponNo(),
					BarcodeFormat.QR_CODE, 260, 260);
			coupondetails_imageview.setImageBitmap(bitmap);
			coupondetails_imageview.setAnimation(animation);
		} catch (WriterException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}

	
	private void adapterContentView(LinearLayout linearLayout) {
		ScrollView contentScrollView = (ScrollView) linearLayout.findViewById(R.id.contentScrollView);
		RelativeLayout titleRelativeLayout = (RelativeLayout) linearLayout.findViewById(R.id.titleRelativeLayout);
		RelativeLayout footerRelativeLayout = (RelativeLayout) linearLayout.findViewById(R.id.footerRelativeLayout);
		LayoutParams contentLayoutParams = contentScrollView.getLayoutParams();
        //獲取屏幕高度
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        
        Integer contentScrollViewHeight = displayMetrics.heightPixels;
        contentLayoutParams.height = contentScrollViewHeight - titleRelativeLayout.getLayoutParams().height - footerRelativeLayout.getLayoutParams().height - 30;
        contentLayoutParams.height = contentScrollViewHeight - 120;
        contentScrollView.setLayoutParams(contentLayoutParams);
        
		setContentView(linearLayout);
	}
	
	
	/**
     * 設置屏幕方向
     */
    private void setScreenOrientation(){
    	getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);		//豎屏
//    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);		//橫屏
    }
	
    private Animation initAnimation(){
		Animation animation = new AlphaAnimation(0.1f, 1.0f); 
		animation.setInterpolator(new AccelerateDecelerateInterpolator());   
		animation.setDuration(2000); 
		return animation;
	}

	
}
